/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Delegate;
import com.bourre.events.BasicEvent;
import com.bourre.events.EventBroadcaster;
import com.bourre.events.EventType;

import fever.data.validators.ValidationResources;
import fever.events.ValidationResultEvent;
import fever.utils.ASType;

/**
 * Base class for all validators.
 * 
 * <p>Take a look at {@code fever.data.validators} package to see all 
 * available validator in Fever.
 * 
 * <p>Use {@link fever.data.validators.ValidationResources} for localisation.
 * 
 * @see fever.data.validators.ValidationResources
 * 
 * @author Romain Ecarnot
 */
class fever.data.validators.Validator extends EventBroadcaster 
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Dispatched when test is not valid. */ 
	public static var INVALID : EventType = ValidationResultEvent.INVALID;
	
	/** Dispatched when test is valid. */ 
	public static var VALID : EventType = ValidationResultEvent.VALID;
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _event : ValidationResultEvent;
	private var _source;
	private var _resources : ValidationResources;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/**  
	 * Setting this value to false will stop the validator from performing 
	 * validation
	 */
	public var enabled : Boolean;
	
	/**
	 * A String specifying the name of the property of the source object that contains 
	 * the value to validate.
	 */
	public var property;
	
	/**
	 * If true, specifies that a missing or empty value causes a validation error.
	 */
	public var required : Boolean;
	
	/**
	 * Specifies the object containing the property to validate.
	 * 
	 * @param object *
	 */
	public function set source( object ) : Void { _setSource(object); }
	
	/**
	 * Returns the object containing the property to validate.
	 * 
	 * @return *
	 */
	public function get source() { return _source; }
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Performs validation and optionally notifies the listeners of the result.
	 * 
	 * @param (optional) value to validate. If null, then the validator uses 
	 * the source and property properties to determine the value.<br />
	 * The default value is {@code null}.<br />
	 * If you specify this argument, you should also set the listener property 
	 * to specify the target component for any validation error messages.
	 * 
	 * @return A {@link fever.events.ValidationResultEvent} object containing 
	 * the results of the validation.
	 */
	public function validate( object ) : ValidationResultEvent
	{
		if(enabled)
		{
			return _doValidation(object);
		}
		return null;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function Validator()
	{
		super( this );
		
		enabled = true;
		property = null;
		required = true;
		
		_event = new ValidationResultEvent();
		_source = null;
		
		_resources = ValidationResources.getInstance();
	}
	
	private function _setSource( object ) : Void
	{
		if(object) _source = object;
	}
	
	private function _validate( event : BasicEvent ) : Void
	{
		_doValidation();
	}
	
	private function _doValidation(object) : ValidationResultEvent
	{
		var toValid = _buildValidationTarget( object );
		
		if( !_testRequired( toValid ) )
		{	
			_fireEventType( Validator.INVALID );
			return _event;
		}
		
		_prepareEvent( Validator.VALID, toValid, "");
		_fireEventType( Validator.VALID );
		
		return _event;	
	}
	
	private function _testRequired(s) : Boolean
	{
		if(required && ( s == undefined || s == null) )
		{
			_prepareEvent( Validator.INVALID, s, _resources.required_field_error );
			return false;
		}
		return true;
	}
	
	private function _buildValidationTarget( object ) 
	{	
		if(object != undefined) return object;
		
		var o;
		switch( typeof(property) )
		{
			case ASType.STRING : 
					o = _source[property]; 
					break;
			case ASType.FUNCTION :
				o = ( new Delegate(_source, property) ).callFunction();
				break;
		}
		return o;
				
	}
	
	private function _prepareEvent( type : EventType, field, message : String) : Void
	{
		_event.setType( type );
		_event.field = field;
		_event.message = message;	
	}
	
	private function _fireEventType( type : EventType ) : Void
	{
		broadcastEvent( _event );
	}
	
}